package wnhz.com.business.groupshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import wnhz.com.business.R;
import wnhz.com.business.uitls.DisplayUtils;
import wnhz.com.business.view.ShowImageDetail;

/**
 * 圈子图片展示adapter
 */
public class QuanZiImageAdapter extends BaseAdapter {

    private Context context;
    private List<String> pics;

    public QuanZiImageAdapter(Context context, List<String> pics) {
        this.context = context;
        this.pics = pics;
    }

    @Override
    public int getCount() {
        return pics.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_imageview, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.id_imageview);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!pics.isEmpty()) {
            String url;
            if(pics.size() == 1 && pics.get(position).startsWith("http://zim.") || pics.get(position).startsWith("http://zgif.")){
                url = pics.get(position) + "@!bi4";
            } else if(pics.size() > 1 && pics.get(position).startsWith("http://zim.") || pics.get(position).startsWith("http://zgif.")){
                url = pics.get(position) + "@!bi3";
            } else {
                url = pics.get(position);
            }
            x.image().bind(holder.imageView, url,shopoptions);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ShowImageDetail.class);
                        intent.putStringArrayListExtra("paths", (ArrayList<String>) pics);
                        intent.putExtra("index", position);
                        context.startActivity(intent);
                }
            });

        }
        return convertView;
    }
    ImageOptions shopoptions = new ImageOptions.Builder()
            .setLoadingDrawableId(R.drawable.banner_default)
            //设置加载失败后的图片
            .setFailureDrawableId(R.drawable.banner_default)
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(false)
            //设置支持gif
            .setIgnoreGif(false)
            .build();
    public class ViewHolder {
        public ImageView imageView;
    }
}
